package com.cst.words.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cst.words.common.R;
import com.cst.words.mapper.SuggestionMapper;
import com.cst.words.mapper.UserMapper;
import com.cst.words.pojo.Suggestion;
import com.cst.words.pojo.User;
import com.cst.words.util.JwtUtils;
import io.jsonwebtoken.Claims;
import jdk.nashorn.internal.ir.JumpStatement;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author : CST
 */
@Slf4j
@RestController
@RequestMapping("/suggestion")
public class SuggestionController implements HandlerInterceptor {

    @Autowired
    private SuggestionMapper suggestionMapper;

    @Autowired
    private UserMapper userMapper;

    @PostMapping("/saveSuggestionInfo")
    public R<String> saveUserInfo(HttpServletRequest request,@RequestBody Suggestion suggestion) {
        // 获得openid
        String token = request.getHeader("token");
        Claims body = JwtUtils.parseJWT(token);
        String string = JSON.toJSONString(body);
        JSONObject jsonObject = JSONObject.parseObject(string);
        String openid = jsonObject.getString("openid");
        // User user = new User();
        // String openid = userMapper.getOpenIdById(user.getId());
        //存储数据
        log.info(openid+"的用户的建议是"+suggestion);
        suggestion.setOpenid(openid);//jwt shiro springSecurity
        suggestion.setTimestamp(new Date());
        if (suggestionMapper.insert(suggestion) > 0) {
            return R.success("保存用户建议成功");
        } else {
            return R.error("保存用户建议失败");
        }
    }

}
